package com.yiyw.herostory.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyw.herostory.mapper.UserMapper;
import com.yiyw.herostory.pojo.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author kwhua
 * @since 2022-08-21
 */
@Component
public class UserService extends ServiceImpl<UserMapper, UserEntity> {

    @Autowired
    UserMapper userMapper;

    public UserEntity login(UserEntity userEntity) {
        QueryWrapper<UserEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(UserEntity::getUserName, userEntity.getUserName());
        UserEntity entity = userMapper.selectOne(qw);
        if (entity == null) {
            // 注册一个新用户
            userMapper.insert(userEntity);
            return userEntity;
        }else {
            if (!entity.getPassword().equals(userEntity.getPassword())) {
                return null;
            }
        }
        return entity;
    }

    public void updateUserHeroAvatar(UserEntity entity) {
        UpdateWrapper<UserEntity> uw = new UpdateWrapper<>();
        uw.lambda().set(UserEntity::getHeroAvatar, entity.getHeroAvatar())
                .eq(UserEntity::getUserId, entity.getUserId());
        userMapper.update(entity, uw);
    }
}
